package com.cp.admin.modular.system.controller;

import com.cp.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cp.admin.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cp.admin.modular.system.model.CodeDict;
import com.cp.admin.modular.system.service.ICodeDictService;

/**
 * 随机码控制器
 *
 * @author fengshuonan
 * @Date 2020-03-02 14:57:21
 */
@Controller
@RequestMapping("/codeDict")
public class CodeDictController extends BaseController {

    private String PREFIX = "/system/codeDict/";

    @Autowired
    private ICodeDictService codeDictService;

    /**
     * 跳转到随机码首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "codeDict.html";
    }

    /**
     * 跳转到添加随机码
     */
    @RequestMapping("/codeDict_add")
    public String codeDictAdd() {
        return PREFIX + "codeDict_add.html";
    }

    /**
     * 跳转到修改随机码
     */
    @RequestMapping("/codeDict_update/{codeDictId}")
    public String codeDictUpdate(@PathVariable Long codeDictId, Model model) {
        CodeDict codeDict = codeDictService.selectById(codeDictId);
        model.addAttribute("item",codeDict);
        LogObjectHolder.me().set(codeDict);
        return PREFIX + "codeDict_edit.html";
    }

    /**
     * 获取随机码列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return codeDictService.selectList(null);
    }

    /**
     * 新增随机码
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CodeDict codeDict) {
        codeDictService.insert(codeDict);
        return SUCCESS_TIP;
    }

    /**
     * 删除随机码
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long codeDictId) {
        codeDictService.deleteById(codeDictId);
        return SUCCESS_TIP;
    }

    /**
     * 修改随机码
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CodeDict codeDict) {
        codeDictService.updateById(codeDict);
        return SUCCESS_TIP;
    }

    /**
     * 随机码详情
     */
    @RequestMapping(value = "/detail/{codeDictId}")
    @ResponseBody
    public Object detail(@PathVariable("codeDictId") Long codeDictId) {
        return codeDictService.selectById(codeDictId);
    }
}
